package com.sun.dailyprj.view.canvas;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

import com.sun.dailyprj.R;


public class CanvasActivity extends AppCompatActivity{
    private InsProgress mInsPg;
    private InsProgress mInsPg2;
    private InsProgress mInsPg3;
    private ProgressBar mInsPg4;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas);

        mInsPg = findViewById(R.id.ins_pg);
        mInsPg2 = findViewById(R.id.ins_pg2);
        mInsPg3 = findViewById(R.id.ins_pg3);
        mInsPg4 = findViewById(R.id.ins_pg4);
        test();

    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what == 0) {
                mInsPg.setProgress((Integer) msg.obj);
            }else if(msg.what == 1) {
                mInsPg2.setProgress((Integer) msg.obj);
            }else if(msg.what == 2) {
                mInsPg3.setProgress((Integer) msg.obj);
            }else if(msg.what == 3) {
                mInsPg4.setProgress((Integer) msg.obj);
            }
            return false;
        }
    });

    private void test() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i <101; i++) {
                    Message msg = new Message();
                    msg.what = 0;
                    msg.obj = i;
                    mHandler.sendMessage(msg);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i <101; i++) {
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = i;
                    mHandler.sendMessage(msg);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i <101; i++) {
                    Message msg = new Message();
                    msg.what = 2;
                    msg.obj = i;
                    mHandler.sendMessage(msg);
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i <101; i++) {
                    Message msg = new Message();
                    msg.what = 3;
                    msg.obj = i;
                    mHandler.sendMessage(msg);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

}
